/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataEntry.java
 *
 * Created on Oct 14, 2011, 1:04:33 PM
 */

package diva_lm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import sun.nio.cs.ext.TIS_620;

/**
 *
 * @author KMD_Computer Centre
 */
public class DataEntry extends javax.swing.JFrame {

    /** Creates new form DataEntry */
    public DataEntry() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtfSongTitle = new javax.swing.JTextField();
        jtfPublisher = new javax.swing.JTextField();
        jcomboName = new javax.swing.JComboBox();
        jcomboNumber = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfConcertType = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbMusicData = new javax.swing.JTable();
        jbtnShow = new javax.swing.JButton();
        jspnminute = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jspnSecond = new javax.swing.JSpinner();
        jLabel9 = new javax.swing.JLabel();
        jtfAuthor = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Data Entry Form");
        setResizable(false);
        getContentPane().setLayout(null);

        jtfSongTitle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfSongTitleActionPerformed(evt);
            }
        });
        getContentPane().add(jtfSongTitle);
        jtfSongTitle.setBounds(200, 91, 230, 20);
        getContentPane().add(jtfPublisher);
        jtfPublisher.setBounds(200, 131, 230, 20);

        jcomboName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One", "A", "B", "C", "D", "E", "F", "G", " " }));
        getContentPane().add(jcomboName);
        jcomboName.setBounds(200, 11, 120, 20);

        jcomboNumber.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One", "1", "2", "3", "4", "5", "6", "7", ".8", "9" }));
        getContentPane().add(jcomboNumber);
        jcomboNumber.setBounds(200, 51, 120, 20);

        jButton1.setText("Exit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(444, 269, 80, 23);

        jButton2.setText("Cancle");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(326, 269, 90, 23);

        jButton3.setText("Save");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(70, 269, 90, 23);

        jLabel1.setText("Drawer Name           :");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 11, 170, 14);

        jLabel2.setText("Drawer Number        :");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 51, 170, 14);

        jLabel3.setText("Song Title                  :");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 91, 170, 14);

        jLabel4.setText("Publisher                    :");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 131, 170, 14);

        jLabel5.setText("Concert Type             :");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(12, 172, 170, 14);

        jLabel6.setText(" Length Of song         :");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(10, 240, 150, 14);
        getContentPane().add(jtfConcertType);
        jtfConcertType.setBounds(200, 169, 230, 20);

        jtbMusicData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
            }
        ));
        jScrollPane1.setViewportView(jtbMusicData);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 290, 560, 430);

        jbtnShow.setText("Show");
        jbtnShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnShowActionPerformed(evt);
            }
        });
        getContentPane().add(jbtnShow);
        jbtnShow.setBounds(218, 269, 90, 23);
        getContentPane().add(jspnminute);
        jspnminute.setBounds(270, 240, 55, 20);

        jLabel7.setText("Minute");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(220, 240, 32, 14);

        jLabel8.setText("Second");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(340, 240, 35, 14);
        getContentPane().add(jspnSecond);
        jspnSecond.setBounds(380, 240, 57, 20);

        jLabel9.setText("Author                       :");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(14, 200, 150, 14);

        jtfAuthor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfAuthorActionPerformed(evt);
            }
        });
        getContentPane().add(jtfAuthor);
        jtfAuthor.setBounds(200, 200, 230, 20);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-577)/2, (screenSize.height-454)/2, 577, 454);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
         if (jcomboName.getSelectedItem().equals("Select One"))
        {
            JOptionPane.showMessageDialog(null, "Please Select Drawer Name");
            return;
        }
         if (jcomboNumber.getSelectedItem().equals("Select One"))
         {
            JOptionPane.showMessageDialog(null,"Please Select Drawer Number","Warinig",JOptionPane.WARNING_MESSAGE);
            return;
         }
         if (jtfSongTitle.getText().equals(""))
         {
            JOptionPane.showMessageDialog(null,"Please Enter Song Title");
            return;
         }
         if (jtfPublisher.getText().equals(""))
         {
            JOptionPane.showMessageDialog(null, "Please Enter Publisher");
            return;
         }
         if (jtfConcertType.getText().equals(""))
         {
            JOptionPane.showMessageDialog(null,"Please Enter Concert Type");
            return;
         }
         if (jspnminute.getValue().equals(0))
         {
            JOptionPane.showMessageDialog(null,"Please Enter Length of Song" );
            return;
         }
         if (jtfAuthor.getText().equals(""))
         {
             JOptionPane.showMessageDialog(null,"Please Enter Author Name");
             return;
         }
        String musicdata;
        musicdata=jcomboName.getSelectedItem().toString()+",";
        musicdata+=jcomboNumber.getSelectedItem().toString()+",";
        musicdata+=jtfSongTitle.getText()+",";
        musicdata+=jtfPublisher.getText()+",";
        musicdata+=jtfConcertType.getText()+",";
         musicdata+=jtfAuthor.getText()+",";
        musicdata+=jspnminute.getValue().toString()+":"+jspnSecond.getValue().toString();
       

        try
        {
            BufferedWriter myBufferWriter=new BufferedWriter(new FileWriter("MusicData.txt",true));
            myBufferWriter.write(musicdata);
            myBufferWriter.newLine();
            myBufferWriter.flush();
            myBufferWriter.close();
            JOptionPane.showMessageDialog(null,"Save Completed!");
        }
        catch(IOException ioe)
        {
        }
        jcomboName.setSelectedIndex(0);
        jcomboNumber.setSelectedIndex(0);
        jtfSongTitle.setText("");
        jtfPublisher.setText("");
        jtfConcertType.setText("");
        jspnminute.setValue(0);
        jspnSecond.setValue(0);
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jtfSongTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfSongTitleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfSongTitleActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
       this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
         jcomboName.setSelectedIndex(0);
        jcomboNumber.setSelectedIndex(0);
        jtfSongTitle.setText("");
        jtfPublisher.setText("");
        jtfConcertType.setText("");
        jspnminute.setValue(0);
        jspnSecond.setValue(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jtfAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfAuthorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfAuthorActionPerformed

    private void jbtnShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnShowActionPerformed
        // TODO add your handling code here:
        this.createMusicDataTableColum();
        try
        {
            this.CommonClass=new ArrayList<CommonClass>();
            BufferedReader myreader = new BufferedReader(new FileReader("MusicData.txt"));
            String readerhead;
            while((readerhead= myreader.readLine())!=null)
            {
                String [] musicdata= readerhead.split(",");
               CommonClass detailCommonClassdata=new CommonClass();
               detailCommonClassdata.DrawerName=musicdata[0];
               detailCommonClassdata.DrawerNo=musicdata[1];
               detailCommonClassdata.SongTitle=musicdata[2];
               detailCommonClassdata.Publisher=musicdata[3];
               detailCommonClassdata.ConcertType=musicdata[4];
               detailCommonClassdata.Author=musicdata[5];
               detailCommonClassdata.LengthOfSong=musicdata[6];
               CommonClass.add(detailCommonClassdata);
               this.createMuscDataTableRow(readerhead);

            }
            myreader.close();
            jtbMusicData.setModel(this.MusicData);
        }
        catch(IOException ioe)
        {

        }
    }//GEN-LAST:event_jbtnShowActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DataEntry().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnShow;
    private javax.swing.JComboBox jcomboName;
    private javax.swing.JComboBox jcomboNumber;
    private javax.swing.JSpinner jspnSecond;
    private javax.swing.JSpinner jspnminute;
    private javax.swing.JTable jtbMusicData;
    private javax.swing.JTextField jtfAuthor;
    private javax.swing.JTextField jtfConcertType;
    private javax.swing.JTextField jtfPublisher;
    private javax.swing.JTextField jtfSongTitle;
    // End of variables declaration//GEN-END:variables
private javax.swing.table.DefaultTableModel MusicData;
private java.util.ArrayList<CommonClass> CommonClass;

private void createMusicDataTableColum()
{
    this.MusicData = new  javax.swing.table.DefaultTableModel();
    this.MusicData.addColumn("Drawer Name");
    this.MusicData.addColumn("Drawer No");
    this.MusicData.addColumn("Song Title");
    this.MusicData.addColumn("Publisher");
    this.MusicData.addColumn("Concert Type");
    this.MusicData.addColumn("Author");
    this.MusicData.addColumn("Length Of Song");
}

private void createMuscDataTableRow(String Row)
{
    String[] myRow=Row.split(",");
    Object[] Data={
    myRow[0],myRow[1],myRow[2],myRow[3],myRow[4],myRow[5],myRow[6]};

    this.MusicData.addRow(myRow);
    

}

}
